package com.cellngine.client;

import java.util.ArrayList;
import java.util.List;

public class Map
{
	private String			title;
	private String			auhor;
	private String			description;
	private short			sizeBytes;
	private List<Layer>		layers;
	private TextureManager	tm;
	private boolean			drawGrid;
	private Layer			grid;
	
	public Map(final TextureManager tm)
	{
		layers = new ArrayList<Layer>();
		this.tm = tm;
		
		grid = new Layer();
		for (short x = 0; x < 128; x++)
		{
			for (short y = 0; y < 128; y++)
			{
				grid.addTile((short) tm.getTexture("grid").getTextureOpenGLIndex(), x, y);
			}
		}
	}
	
	public void load(final String Path)
	{
		// TODO
	}
	
	public void generate()
	{
		final Layer layer0 = new Layer();
		final Layer layer1 = new Layer();
		
		// Space background (filled layers)
		for (short x = 0; x < 128; x++)
		{
			for (short y = 0; y < 128; y++)
			{
				layer0.addTile((short) tm.getTexture("space").getTextureOpenGLIndex(), x, y);
			}
		}
		
		// Misc layer (linked layer)
		
		// L
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 1,
				(short) 1);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 1,
				(short) 2);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 1,
				(short) 3);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 2,
				(short) 3);
		
		// O
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 4,
				(short) 1);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 5,
				(short) 1);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 6,
				(short) 1);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 4,
				(short) 2);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 6,
				(short) 2);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 4,
				(short) 3);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 5,
				(short) 3);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 6,
				(short) 3);
		
		// L
		
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 8,
				(short) 1);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 8,
				(short) 2);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 8,
				(short) 3);
		layer1.addTile((short) tm.getTexture("floor_grille").getTextureOpenGLIndex(), (short) 9,
				(short) 3);
		
		layers.add(layer0);
		layers.add(layer1);
		
		final int totalSize = layer0.getSize() + layer1.getSize();
		
		System.out.println("Generated " + layers.size() + " layers containing " + totalSize
				+ " tiles.");
		
	}
	
	public void draw()
	{
		for (short i = 0; i < layers.size(); i++)
		{
			layers.get(i).draw();
		}
		
		if (drawGrid)
		{
			grid.draw();
		}
	}
	
	public void toggleGrid()
	{
		drawGrid = !drawGrid;
	}
}
